import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:ml_flutter/pages/const.dart';



enum RequestType { POST, GET }

class MLReauestModel {
  static final MLReauestModel shareModel = MLReauestModel.shareInstance();
  Dio client;
  MLReauestModel.shareInstance() {
    if (client == null) {
      
      Map<String,dynamic> headers = {
        "Content-Type": "application/json;charset=UTF-8",
        "responseType": ResponseType.json,
      };

      BaseOptions options = BaseOptions(
        headers: headers,
        baseUrl: MainUrl,
        connectTimeout: 5000,
        receiveTimeout: 3000,
        contentType:
            ContentType.parse('application/x-www-form-urlencoded').toString(),
      );
      client = Dio(options);
    }
  }

  Future <dynamic> requestUrl(RequestType type,String url,[Map<String,dynamic> param]) async{
  Response response;
  if(type == RequestType.POST){
   if(param == null){
     response  = await client.post(url);
   }else{
      response  = await client.post(url, data: param);
   }
  }else{
     response  = await client.get(url);
  }

  var data;
  if(response.statusCode == 200){
      
    data = json.decode(response.toString());
  }else{
      throw Exception('statusCode:${response.statusCode}');
  }
  return data;
  }
}
